﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.Serialization;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
/// <summary>
/// 扩展方法
/// </summary>
public static class Extensions
{
    public static void ObjToXml(this object obj,string xmlpath)
    {        
        XmlTextWriter writer = new XmlTextWriter(xmlpath, Encoding.Default);
        XmlSerializer ser = new XmlSerializer(obj.GetType());
        ser.Serialize(writer, obj);
        writer.Close();
    }

    public static T XmlToObj<T>(this string xmlpath)
    {
        XmlSerializer ser = new XmlSerializer(typeof(T));
        XmlReader reader = new XmlTextReader(xmlpath);
        T obj= (T)ser.Deserialize(reader);
        reader.Close();
        return obj;
    }

    public static string ToJSON(this object obj)
    {
        JavaScriptSerializer s = new JavaScriptSerializer();
        string json = s.Serialize(obj);

        return json;
    }

    public static T ToOBJ<T>(this string json)
    {
        if (string.IsNullOrEmpty(json))
        {
            json = "[]";
        }
        JavaScriptSerializer s = new JavaScriptSerializer();
        return s.Deserialize<T>(json);
    }

    public static string ToSplitString(this string[] Array)
    {
        if (Array == null)
        {
            return string.Empty;
        }
        string r = ",";
        foreach (string str in Array)
        {
            r += str + ",";
        }

        return r.Replace(",,", ",");
    }

    public static string ToSplitTrimString(this string[] Array)
    {
        return Array.ToSplitString().Trim(",".ToCharArray());
    }

    public static string[] TrimSplit(this string str)
    {
        return str.Trim(",".ToCharArray()).Split(',');
    }

    public static void CheckAll(this CheckBoxList checklist, string[] checkValue)
    {
        if (checkValue == null)
        {
            return;
        }
        checklist.ClearSelection();
        foreach (ListItem item in checklist.Items)
        {
            foreach (string str in checkValue)
            {
                if (item.Value == str)
                {
                    item.Selected = true;
                    break;
                }
            }
        }
    }

    public static string GetCheckedValues(this CheckBoxList checklist)
    {
        string r = ",";
        foreach (ListItem item in checklist.Items)
        {
            if (item.Selected)
            {
                r += item.Value + ",";
            }
        }
        return r;
    }

    public static string ToCnWeek(this DayOfWeek week)
    {
        switch (week)
        {
            case DayOfWeek.Monday:
                return "星期一";
                break;
            case DayOfWeek.Tuesday:
                return "星期二";
                break;
            case DayOfWeek.Wednesday:
                return "星期三";
                break;
            case DayOfWeek.Thursday:
                return "星期四";
                break;
            case DayOfWeek.Friday:
                return "星期五";
                break;
            case DayOfWeek.Saturday:
                return "星期六";
                break;
            case DayOfWeek.Sunday:
                return "星期日";
                break;
            default:
                return "";
        }
    }

    public static string ToSql(this string src)
    {
        if (!string.IsNullOrEmpty(src))
        {
            return src.Replace("'", "''").Replace("<script", HttpContext.Current.Server.HtmlEncode("<script")).Replace("</script>", HttpContext.Current.Server.HtmlEncode("</script>"));
        }
        else
        {
            return string.Empty;
        }
    }

    public static string DecodeScript(this string src)
    {
        return src.Replace("&lt;script", "<script").Replace("&lt;/script&gt;","</script>");
    }

    public static string GetQueryValue(this Uri uri, string paramname)
    {
        string qstring = string.Empty;
        if (uri.IsAbsoluteUri)
        {
            qstring = uri.Query;
        }
        else
        {
            qstring = uri.ToString().Split('?').Last();
        }
        string[] qs = qstring.Split('&');
        foreach (string q in qs)
        {
            string[] vs = q.Split('=');
            if (vs.Length == 2)
            {
                if (vs[0] == paramname)
                {
                    return vs[1];
                }
            }
            else
            {
                continue;
            }
        }
        return string.Empty;
    }
}